package com.codeant.bikeindicatorapplication;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;

import androidx.core.app.ActivityCompat;
import com.allen.library.SuperButton;
import com.allen.library.SuperTextView;
import com.codeant.bikeindicatorapplication.tools.ClickFeedbackHelper;
import com.codeant.bikeindicatorapplication.tools.PublicVariables;
import com.codeant.bikeindicatorapplication.tools.SharedPreferencesHelper;
import com.codeant.bikeindicatorapplication.tools.SingleToast;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopups;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class SettingActivity extends Activity {
    
    private String TAG = "SettingActivity";

    private SuperButton statusbar_sbtn;
    private SuperTextView title_stv;
    private SuperTextView restart_stv;//重启APP按钮
    private SuperTextView help_stv;//帮助按钮
    private SuperTextView ble_stv;//蓝牙调试按钮
    private SuperTextView perms_stv;
    private SuperTextView ss_calibrate_stv;//陀螺仪校准
    private SuperTextView honking_plus_stv;//增强鸣笛按钮
//    private SuperTextView ss_status_stv;
//    private SuperTextView app_version_stv;
    private SuperTextView land_mode_stv;//横屏模式
    private SuperTextView left_mode_stv;//左手模式
    private SuperTextView right_mode_stv;//右手模式

    private ClickFeedbackHelper clickFeedbackHelper;

    private Integer TAG_STV_TITLE = 1;
    private Integer TAG_STV_HELP = 2;
    private Integer TAG_STV_BLE = 3;
    private Integer TAG_STV_LAND = 4;
    private Integer TAG_STV_LAND_SWITCH = 41;
    private Integer TAG_STV_LEFT = 5;
    private Integer TAG_STV_LEFT_SWITCH = 51;
    private Integer TAG_STV_RIGHT = 42;
    private Integer TAG_STV_RIGHT_SWITCH = 422;
    private Integer TAG_STV_SS_CALI = 6;
    private Integer TAG_STV_RESTART = 7;
    private Integer TAG_STV_HONKING = 8;
    private Integer TAG_STV_PERMS = 9;
    private Integer TAG_STV_HONKING_SWITCH = 81;

    PublicVariables pubVari = new PublicVariables();
    SharedPreferencesHelper sph = new SharedPreferencesHelper();//信息存储工具

    private boolean SHOW_REMIND_RESTART = false;//标记是否已弹出重启的提醒，避免重复弹出

    protected void onCreate(Bundle savedInstanceState) {
        sph.initSP(SettingActivity.this);//记得初始化
        super.onCreate(savedInstanceState);
//        QMUIStatusBarHelper.translucent(SettingActivity.this);
        setContentView(R.layout.activity_setting);

        initWidgets();// 初始化控件

        title_stv.setOnClickListener(new ClickListeners());
        title_stv.setTag(TAG_STV_TITLE);

        restart_stv.setOnClickListener(new ClickListeners());
        restart_stv.setTag(TAG_STV_RESTART);

        help_stv.setOnClickListener(new ClickListeners());
        help_stv.setTag(TAG_STV_HELP);

        ble_stv.setOnClickListener(new ClickListeners());
        ble_stv.setTag(TAG_STV_BLE);

        land_mode_stv.setOnClickListener(new ClickListeners());
        land_mode_stv.setTag(TAG_STV_LAND);
        land_mode_stv.setSwitchCheckedChangeListener(new SwitchCheckedChangeListener());
        land_mode_stv.getSwitch().setTag(TAG_STV_LAND_SWITCH);

        left_mode_stv.setOnClickListener(new ClickListeners());
        left_mode_stv.setTag(TAG_STV_LEFT);
        left_mode_stv.setSwitchCheckedChangeListener(new SwitchCheckedChangeListener());
        left_mode_stv.getSwitch().setTag(TAG_STV_LEFT_SWITCH);

        right_mode_stv.setOnClickListener(new ClickListeners());
        right_mode_stv.setTag(TAG_STV_RIGHT);
        right_mode_stv.setSwitchCheckedChangeListener(new SwitchCheckedChangeListener());
        right_mode_stv.getSwitch().setTag(TAG_STV_RIGHT_SWITCH);

        honking_plus_stv.setOnClickListener(new ClickListeners());
        honking_plus_stv.setTag(TAG_STV_HONKING);
        honking_plus_stv.setSwitchCheckedChangeListener(new SwitchCheckedChangeListener());
        honking_plus_stv.getSwitch().setTag(TAG_STV_HONKING_SWITCH);

        ss_calibrate_stv.setOnClickListener(new ClickListeners());
        ss_calibrate_stv.setTag(TAG_STV_SS_CALI);

        perms_stv.setOnClickListener(new ClickListeners());
        perms_stv.setTag(TAG_STV_PERMS);
    }

    // 初始化控件
    private void initWidgets(){
        statusbar_sbtn = (SuperButton) findViewById(R.id.statusbar_sbtn_setting);
        title_stv = (SuperTextView) findViewById(R.id.title_stv_setting);
        restart_stv = (SuperTextView) findViewById(R.id.restart_stv_setting);
        honking_plus_stv = (SuperTextView) findViewById(R.id.honking_plus_stv_setting);
        help_stv = (SuperTextView) findViewById(R.id.help_stv_setting);
        ble_stv = (SuperTextView) findViewById(R.id.ble_stv_setting);
        perms_stv = (SuperTextView) findViewById(R.id.perms_stv_setting);
        land_mode_stv = (SuperTextView) findViewById(R.id.land_stv_setting);
        left_mode_stv = (SuperTextView) findViewById(R.id.left_mode_stv_setting);
        right_mode_stv = (SuperTextView) findViewById(R.id.right_mode_stv_setting);
        ss_calibrate_stv = (SuperTextView) findViewById(R.id.ss_calibrate_stv_setting);

        restart_stv.setVisibility(View.GONE);//将重启按钮设置为不可见

        //权限检查
        if (ActivityCompat.checkSelfPermission(
                this,
                pubVari.getPerms()[0]) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(
                this,
                pubVari.getPerms()[1]) != PackageManager.PERMISSION_GRANTED
        ){
            perms_stv.setRightString("缺少权限");
//            perms_stv.setRightTextColor(R.color.red);//红色字体更加醒目
        }else{
            perms_stv.setRightString("正常");
//            perms_stv.setRightTextColor(R.color.blue);
        }

        clickFeedbackHelper = new ClickFeedbackHelper();

        int statusBarHeight_pixel = getIntent().getIntExtra("statusBarHeight_pixel",10);
        Log.i(TAG, String.valueOf(statusBarHeight_pixel));
        statusbar_sbtn.setHeight(statusBarHeight_pixel);

        //设置标题字体加粗
        title_stv.setLeftTextIsBold(true);

        //横屏模式右侧开关相关事件
        boolean land_mode = sph.getSP_boolean("BI","land_mode",false);
        land_mode_stv.setSwitchIsChecked(land_mode);

        //左手模式右侧开关相关事件
        boolean left_mode = sph.getSP_boolean("BI","left_mode",false);
        left_mode_stv.setSwitchIsChecked(left_mode);

        //左手模式右侧开关相关事件
        boolean right_mode = sph.getSP_boolean("BI","right_mode",false);
        right_mode_stv.setSwitchIsChecked(right_mode);

        //增强鸣笛右侧开关相关事件
        boolean honking_plus = sph.getSP_boolean("BI","honking_plus",false);
        honking_plus_stv.setSwitchIsChecked(honking_plus);
    }

    private class ClickListeners implements View.OnClickListener{
        @Override
        public void onClick(View v){
            //让所有可点击事件都带有震动反馈
            clickFeedbackHelper.feedback(v, SettingActivity.this);

            int key = 0;
            if(v.getTag()!=null){
                key = (Integer) v.getTag();
            }
            switch (key){
                case 1://返回上一页
                    finish();
                    Log.d(TAG,"tag_1");
                    break;
                case 2://帮助
                    new QMUIDialog.MessageDialogBuilder(SettingActivity.this)
                            .setTitle("提示")
                            .setMessage(R.string.str_setting_help)
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    clickFeedbackHelper.feedback(v, SettingActivity.this);
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    Log.d(TAG,"tag_2");
                    break;
                case 3://蓝牙调试
                    Intent it = new Intent(SettingActivity.this, BleDebugActivity.class);
                    startActivity(it);
                    Log.d(TAG,"tag_3");
                    break;
                case 4://横屏模式开关
                    new QMUIDialog.MessageDialogBuilder(SettingActivity.this)
                            .setTitle("提示")
                            .setMessage(R.string.str_setting_change_ui_tips)
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    clickFeedbackHelper.feedback(v, SettingActivity.this);
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                case 5://左手布局的开关
                    new QMUIDialog.MessageDialogBuilder(SettingActivity.this)
                            .setTitle("提示")
                            .setMessage(R.string.str_setting_change_ui_tips)
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    clickFeedbackHelper.feedback(v, SettingActivity.this);
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                case 42://右手布局的开关
                    new QMUIDialog.MessageDialogBuilder(SettingActivity.this)
                            .setTitle("提示")
                            .setMessage(R.string.str_setting_change_ui_tips)
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    clickFeedbackHelper.feedback(v, SettingActivity.this);
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                case 6://陀螺仪校准
                    Intent intent = new Intent(SettingActivity.this, SsCalibrateActivity.class);
                    startActivity(intent);
                    Log.d(TAG,"tag_6");
                    break;
                case 7://重启APP
                    pubVari.restart(SettingActivity.this,SettingActivity.this);
                    Log.d(TAG,"tag_6");
                    break;
                case 8://增强鸣笛
                    new QMUIDialog.MessageDialogBuilder(SettingActivity.this)
                            .setTitle("提示")
                            .setMessage(R.string.str_setting_honking_plus_tips)
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    clickFeedbackHelper.feedback(v, SettingActivity.this);
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    Log.d(TAG,"tag_8");
                    break;
                case 9://权限管理
                    Intent localIntent = new Intent();
                    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    if (Build.VERSION.SDK_INT >= 9) {
                        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                        localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                    } else if (Build.VERSION.SDK_INT <= 8) {
                        localIntent.setAction(Intent.ACTION_VIEW);
                        localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                        localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
                    }
                    startActivity(localIntent);
                    Log.d(TAG,"tag_9");
                    break;
                default:
                    break;
            }
        }
    }

    private class SwitchCheckedChangeListener implements SuperTextView.OnSwitchCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //让所有可点击事件都带有震动反馈
            clickFeedbackHelper.feedback(buttonView, SettingActivity.this);

            int key = 0;
            if(buttonView.getTag()!=null){
                key = (Integer) buttonView.getTag();
            }
            switch (key) {
                case 41://横屏
                    if (isChecked){
                        sph.saveSP_boolean("BI","land_mode",true);
                    }else{
                        sph.saveSP_boolean("BI","land_mode",false);
                    }
//                    restart_stv.setVisibility(View.VISIBLE);//更改重启按钮可见性
                    remindRestart(buttonView);
                    break;
                case 51://左手
                    if (isChecked){
                        sph.saveSP_boolean("BI","left_mode",true);
                        if (right_mode_stv.getSwitchIsChecked()){//避免左右冲突，二者只能取其一或者都为空
                            right_mode_stv.setSwitchIsChecked(false);
                            sph.saveSP_boolean("BI","right_mode",false);
                        }
                    }else{
                        sph.saveSP_boolean("BI","left_mode",false);
                    }
//                    restart_stv.setVisibility(View.VISIBLE);//更改重启按钮可见性
                    remindRestart(buttonView);
                    break;
                case 422://右手
                    if (isChecked){
                        sph.saveSP_boolean("BI","right_mode",true);
                        if (left_mode_stv.getSwitchIsChecked()){//避免左右冲突，二者只能取其一或者都为空
                            left_mode_stv.setSwitchIsChecked(false);
                            sph.saveSP_boolean("BI","left_mode",false);
                        }
                    }else{
                        sph.saveSP_boolean("BI","right_mode",false);
                    }
//                    restart_stv.setVisibility(View.VISIBLE);//更改重启按钮可见性
                    remindRestart(buttonView);
                    break;
                case 81://鸣笛
                    if (isChecked){
                        sph.saveSP_boolean("BI","honking_plus",true);
                    }else{
                        sph.saveSP_boolean("BI","honking_plus",false);
                    }
//                    restart_stv.setVisibility(View.VISIBLE);//更改重启按钮可见性
                    remindRestart(buttonView);
                    break;
                default:
                    break;
            }
        }
    };

    public void remindRestart(View v){
        if (!SHOW_REMIND_RESTART){
            new QMUIDialog.MessageDialogBuilder(SettingActivity.this)
                    .setTitle("提示")
                    .setMessage("设置变更后需要重启APP生效，现在重启吗？")
                    .addAction("稍后", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            clickFeedbackHelper.feedback(v, SettingActivity.this);
                            dialog.dismiss();
                            SHOW_REMIND_RESTART = false;
                        }
                    })
                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            clickFeedbackHelper.feedback(v, SettingActivity.this);
                            dialog.dismiss();
                            SHOW_REMIND_RESTART = false;
                            pubVari.restart(SettingActivity.this,SettingActivity.this);
                        }
                    })
                    .show();
            SHOW_REMIND_RESTART = true;
        }
    }

}

